package mc.support.web.action.invoke;

import mc.support.web.action.ActionWrapper;
import mc.support.web.action.aware.FileDownloadAware;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created with IntelliJ IDEA. <br/>
 * Author: Francis Yun    <br/>
 * Date: 2014-04-21  <br/>
 */
public class FileDownloadActionInvoker extends InvokerSupport implements Invoker<FileDownloadAware> {

    @Override
    public int getPriority() {
        return ActionPriority.FILE_DOWNLOAD;
    }

    @Override
    public Class<FileDownloadAware> getAwareClass() {
        return FileDownloadAware.class;
    }

    @Override
    public Object invoke(FileDownloadAware aware, ActionWrapper action, Map<String, Object> params) throws Exception {
        HttpServletResponse resp = getHttpResp( params );
        String fileType = null == params.get("fileType") ? "csv":params.get("fileType").toString();
        String fileName = aware.getFileName() + "." + fileType;
        resp.setHeader("Content-disposition","attachment; filename="+fileName);
        return aware.doDownload( params );
    }

    @Override
    public ResultWriter getResultWriter() {
        return ResultWriter.binary;
    }
}
